package com.newbingo.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IAgentLogDao;
import com.newbingo.entity.AgentLog;
import com.newbingo.util.Pager;

public class AgentLogDao extends HibernateDaoSupport implements IAgentLogDao {
	private static final Log log=LogFactory.getLog(AgentDao.class);
	
	/* 
	 * 保存历史操作信息
	 */
	public void save(AgentLog agentLog){
		log.debug("saving AgentLog instance");
		try{
			getSession().save(agentLog);
			log.info("save successfully");
		}catch(RuntimeException re){
			log.debug("saving failed");
			throw re;
		}
	}
	
	/* 
	 * 根据代理的ID查询历史操作记录并进行分页显示
	 */
	public Pager findByPage(int start,int limit,Integer uno){
		log.debug("finding AgentLog by uno and pager");
		try{
			Pager page=new Pager();
			Criteria cri=getSession().createCriteria(AgentLog.class);
			if(uno!=null){
				cri.createAlias("agent","agent");
				cri.add(Expression.eq("agent.uno", uno));
			}
			cri.setProjection(Projections.rowCount());
			page.setTotalSize(((Number)cri.uniqueResult()).intValue());
			cri.setProjection(null);
			cri.setResultTransformer(Criteria.ROOT_ENTITY);
			cri.setFirstResult(start);
			cri.setMaxResults(limit);
			page.setData(cri.list());
			return page;
		}catch(RuntimeException re){
			log.error("finding error");
			throw re;
		}
	}
	
}
